# -*- coding: utf-8 -*-
import web
import time
import win32com.client
urls = ("/", "login",
        "/login.html", "login",
        "/mainpage.html", "main",
        "/confirm","confirm",
        "/logout","logout",
        "/newreg","newreg"
        )
app = web.application(urls, globals())
gbv={'ctime':time.ctime}
conn = win32com.client.Dispatch(r'ADODB.Connection')
DSN = 'PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE\
        =usrs.mdb;jet OleDB:Database Password=ca'
conn.Open(DSN)
class logout:
    def GET(self):
        web.setcookie('usrname','游客')
        raise web.seeother('/')
class login:
    def GET(self):
        render=web.template.frender("templates/login.html")
        return render('请输入用户名和密码')
class main:
    def GET(self):
        render=web.template.frender("templates/mainpage.html",globals=gbv)
        un=web.cookies(usrname='游客')
        return render(un.usrname)
class confirm:
    def POST(self):
        data=web.input()
        if data.usrname=='':
            data.usrname='游客'
            web.setcookie('usrname',data.usrname)
            raise web.seeother('/mainpage.html')
        else:
            sqlquest='select pw from usrs where name="'+data.usrname+'"'
            rs=conn.Execute(sqlquest)[0]
            if rs.EOF or rs.BOF:
                render=web.template.frender("templates/login.html")
                return render('用户名或密码错误')
            else:
                rs.MoveFirst()
                if data.pwd==rs.Fields(0).Value:
                    web.setcookie('usrname',data.usrname)
                    raise web.seeother('/mainpage.html')
class newreg:
    def GET(self):
        return "new reg!"           

if __name__ == "__main__":
    app.run()
